package com.hmall.gateway.filter;

import com.fasterxml.jackson.databind.deser.impl.ObjectIdReader;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cloud.gateway.filter.GatewayFilterChain;
import org.springframework.cloud.gateway.filter.GlobalFilter;
import org.springframework.core.Ordered;
import org.springframework.core.annotation.Order;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Component;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;

/**
 * 全局过滤器
 *
 * @Author wd
 * @Date 2025/6/20 11:24
 */
@Slf4j
//@Component
public class MyGlobalFilter implements GlobalFilter, Ordered {

    /**
     * 实现针对请求的过滤逻辑
     * @param exchange
     * @param chain
     * @return
     */
    @Override
    public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {

        log.info("收到请求，请求路径：{}",exchange.getRequest().getURI().getPath() );

        //放行请求
        //return chain.filter(exchange);

        //拒绝请求，直接响应结束
        exchange.getResponse().setStatusCode(HttpStatus.UNAUTHORIZED);//响应401
        return exchange.getResponse().setComplete();
    }

    //指定过滤器执行优先级，值越小优先级越高
    @Override
    public int getOrder() {
        return 0;
    }
}
